package com.sas.hnn;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyIntentService extends IntentService {

	private static final String TAG = IntentService.class.getSimpleName();
	private static final int HELLO_ID = 1;

	/**
	 * A constructor is required, and must call the super IntentService(String)
	 * constructor with a name for the worker thread.
	 */
	public MyIntentService() {
		super("HelloIntentService");
	}

	/**
	 * The IntentService calls this method from the default worker thread with
	 * the intent that started the service. When this method returns,
	 * IntentService stops the service, as appropriate.
	 */
	protected void onHandleIntent(Intent intent) {
		Log.d(TAG, "in onHandleIntent");
		// Normally we would do some work here, like download a file.
		// For our sample, we just sleep for 5 seconds.
		long endTime = System.currentTimeMillis() + 10 * 1000;
		while (System.currentTimeMillis() < endTime) {
			synchronized (this) {
				try {
					Log.i(IntentService.class.getSimpleName(),
							"Anfang des Handler vor 10.000ms");
					wait(endTime - System.currentTimeMillis());
					Log.i(IntentService.class.getSimpleName(), "Nach 10.000ms");

				} catch (Exception e) {
				}

			}
		}

		// START Notification
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

		int icon = R.drawable.ic_launcher;
		CharSequence tickerText = "Intent Notification nach 10s";
		long when = System.currentTimeMillis();

		Notification notification = new Notification(icon, tickerText, when);

		Context context = getApplicationContext();
		CharSequence contentTitle = "Startet automatisch nach 10s";
		CharSequence contentText = "Der Service ist beendet und wird sich jetzt stoppen.";
		Intent notificationIntent = new Intent(this, MyServiceActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				notificationIntent, 0);

		notification.setLatestEventInfo(context, contentTitle, contentText,
				contentIntent);

		mNotificationManager.notify(HELLO_ID, notification);
		// ENDE Notification
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "in onDestroy()");
	}

	@Override
	public boolean onUnbind(Intent intent) {
		Log.d(TAG, "in onUnbind()");
		return super.onUnbind(intent);
	}

}